<?php 

class AdminMemberPresenter extends AdminPresenter{
	
	private $model;
	/**
	 * Start lifecycle presenter
	 *
	 * @return void
	 */
	protected function startup(){
		parent::startup();
		$this->template->jsFile[] = "inc/news.js";
		$this->model = MemberModel::getInstance();
	}

	/**
	 * Settings for all render
	 *
	 * @return void
	 */
	protected function beforeRender(){

	}

	/**
	 * Render default action
	 *
	 * @return void
	 */
	public function renderDefault($id){
		
		$this->template->title = "Přehled členů";
		$this->template->id = $id;
	}

	protected function createComponentGrid($name){
		
		$grid = new DataGrid;
	    $grid->bindDataTable($this->model->getDataSource());
	    
	    $grid->addColumn("jmeno", "Jméno")->addFilter();
	    $grid->addColumn("nick", "Přezdívka")->addFilter();
	    
	    // nastavíme klíč pro akce (a také i pro operace, o těch později)
		$grid->keyName = 'id_member';
		
		// přidáme sloupec pro akce (sloupců může být i více)
		$grid->addActionColumn('Actions');
		
		// a naplníme datagrid akcemi pomocí továrničky
		$grid->addAction('Nový', 'addMember', Html::el('span')->class('icon icon-edit'), $useAjax = FALSE);
		$grid->addAction('Smaž', 'deleteMember', Html::el('span')->class('icon icon-del'), $useAjax = TRUE);
			    
	    $this->addComponent($grid, "grid");
	}
	
	public function actionAddMember($id){
		
		$member = $this->model->fetchMembers(array("id_member" => $_GET["id_member"]));
		
		$this->template->title = "Přidat člena";
		$this->template->edit = $member[0];
		$this->template->picture = $this->model->getPicture($this->template->edit["id_file"]);
		$this->template->id = $id;
	}
	
	public function actionSaveMember(){
		
		$values = $_POST;
		
		if(isset($values["clen"])) $values["clen"] = 1;
		else $values["clen"] = 0;
		
		$this->model->saveMember($values);
		$this->flashMessage("Údaje o uživateli byly upraveny.", "ok");
		$this->redirect("AdminMember:default", $this->getParam("id"));
	}
	
	public function actionDeleteMember($id_member){
		
		$this->model->deleteMember($id_member);
		$this->flashMessage("Uživatel byl smazán.", 'ok');
		$this->redirect("AdminMember:default", $this->getParam("id"));
	}
	
}

?>
